package hash;

import java.util.Map;

import java.util.Map.Entry;

public class Node<K, V> implements Map.Entry<K, V> {
    final int hash;
    final K key;
    volatile V val;
    volatile Node<K, V> next;

    Node(int hash, K key, V val) {
        this.hash = hash;
        this.key = key;
        this.val = val;
    }

    Node(int hash, K key, V val, Node<K, V> next) {
        this(hash, key, val);
        this.next = next;
    }

    public final K getKey() {
        return this.key;
    }

    public final V getValue() {
        return this.val;
    }

    public final int hashCode() {
        return this.key.hashCode() ^ this.val.hashCode();
    }

    public final String toString() {
        return key + " = " + val;
    }

    public final V setValue(V value) {
        throw new UnsupportedOperationException();
    }

    public final boolean equals(Object o) {
        Object k;
        Object v;
        Object u;
        Entry e;
        return o instanceof Entry && (k = (e = (Entry) o).getKey()) != null && (v = e.getValue()) != null && (k == this.key || k.equals(this.key)) && (v == (u = this.val) || v.equals(u));
    }

    Node<K, V> find(int h, Object k) {
        Node<K, V> e = this;
        if (k != null) {
            do {
                Object ek;
                if (e.hash == h && ((ek = e.key) == k || ek != null && k.equals(ek))) {
                    return e;
                }
            } while ((e = e.next) != null);
        }

        return null;
    }
}